﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MayhemCore;
using MayhemCore.ModuleTypes;
using System.Device.Location;

namespace MayhemWP7.Modules
{
    public abstract class GeoLocationBase : EventBase
    {
        private GeoCoordinateWatcher watcher;

        #region Configuration Properties
        // this is the center of the trigger circle
        private GeoCoordinate _center;
        public GeoCoordinate Center
        {
            get
            {
                return _center;
            }
            set
            {
                _center = value;
                SetConfigString();
            }
        }

        private int _radius;
        public int Radius
        {
            get
            {
                return _radius;
            }
            set
            {
                _radius = value;
                SetConfigString();
            }
        }
        #endregion

        protected GeoCoordinate PrevLocation;

        public GeoLocationBase()
        {
        }

        protected override void Initialize()
        {
            base.Initialize();

            //Use of High uses more battery but includes GPS. Low uses lower power and only uses cell triangulation
            watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);

            //This is the frequency you want to be informed about in meters. In this case we are telling
            //our object to fire the PositionChanged event every time we move by 1 meters.
            watcher.MovementThreshold = 1;

            watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);

            // Set defaults
            Center = new GeoCoordinate(0, 0);
            Radius = 5;
        }

        void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() => MyPositionChanged(e));
        }

        void MyPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            // Get our current location
            GeoCoordinate coord = e.Position.Location;

            CheckCoord(coord);

            PrevLocation = coord;
        }

        protected abstract void CheckCoord(GeoCoordinate coord);

        public override void SetConfigString()
        {
            // if center is null then return
            if (Center == null) {
                return;
            }

            ConfigString = String.Format("{0} meters from {1},{2}", Radius, Center.Latitude.ToString("#.######"), Center.Longitude.ToString("#.######"));
        }

        public override void Enable()
        {
            // if for some reason we haven't been fully configured yet, then don't do anything
            if (Center == null) {
                ErrorLog.AddError(ErrorType.Warning, "Center Location hasn't been configured");
                return;
            }

            base.Enable();
            watcher.Start();
        }

        public override void Disable()
        {
            base.Disable();
            watcher.Stop();
        }
    }
}
